package com.leyou.order.controller;

import com.leyou.common.dto.PageResult;
import com.leyou.order.dto.OrderDTO;
import com.leyou.order.dto.OrderVO;
import com.leyou.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/order")
    public ResponseEntity<Long> saveOrder(@RequestBody OrderDTO orderDTO){
        Long orderId = orderService.saveOrder(orderDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(orderId);
    }

    /**
     * 查询订单信息
     */
    @GetMapping("/order/{id}")
    public ResponseEntity<OrderVO> findOrderVoById(@PathVariable("id") Long id){
        OrderVO orderVO = orderService.findOrderVoById(id);
        return ResponseEntity.ok(orderVO);
    }

    /**
     * 获取支付链接的处理器
     */
    @GetMapping("/order/url/{id}")
    public ResponseEntity<String> getPayUrl(@PathVariable("id") Long id){
        String payUrl = orderService.getPayUrl(id);
        return ResponseEntity.ok(payUrl);
    }

    /**
     * 查询支付状态
     */
    @GetMapping("/order/state/{id}")
    public ResponseEntity<Integer> getPayCode(@PathVariable("id") Long id){
        Integer status = orderService.getPayCode(id);
        return ResponseEntity.ok(status);
    }
    /**
     * 根据订单状态分页查询订单
     */
    @GetMapping("/order/status/skus")
    public ResponseEntity<PageResult<OrderVO>> queryOrderPageByStatus(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                      @RequestParam(value = "rows", defaultValue = "1") Integer rows,
                                                                      @RequestParam(value = "key", required = false) String key,
                                                                      @RequestParam(value = "status",required = false) Integer status){
        PageResult<OrderVO> pageResult = orderService.queryOrderPageByStatus(page,rows,key,status);
        return ResponseEntity.ok(pageResult);
    }
    /**
     * 根据订单状态查数量
     */
    @GetMapping("/order/status")
    public ResponseEntity<Integer> queryOrderCountByStatus(@RequestParam("status") Integer status){
       Integer count =  orderService.queryOrderCountByStatus(status);
       return ResponseEntity.ok(count);
    }
    /**
     * 修改订单状态
     */
    @PutMapping("/order/status/update")
    public ResponseEntity<Void> updateOrderStatusById(@RequestParam("orderId") Long orderId){
        orderService.updateOrderStatusById(orderId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
